package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.ServiceContent;
import org.dromara.system.domain.ServiceContentTalent;
import org.dromara.system.domain.bo.ServiceContentBo;
import org.dromara.system.domain.bo.ServiceContentLevelReqBo;
import org.dromara.system.domain.vo.ServiceContentVo;
import org.dromara.system.mapper.ServiceContentMapper;
import org.dromara.system.mapper.ServiceContentTalentMapper;
import org.dromara.system.service.IServiceContentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 服务内容Service业务层处理
 *
 * @author 毛宁川
 * @date 2025-07-23
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ServiceContentServiceImpl implements IServiceContentService {

    private final ServiceContentMapper baseMapper;

    private final ServiceContentTalentMapper serviceContentTalentMapper;

    /**
     * 查询服务内容
     *
     * @param serviceContentId 主键
     * @return 服务内容
     */
    @Override
    public ServiceContentVo queryById(Long serviceContentId) {
        return baseMapper.selectVoById(serviceContentId);
    }

    /**
     * 分页查询服务内容列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 服务内容分页列表
     */
    @Override
    public TableDataInfo<ServiceContentVo> queryPageList(ServiceContentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ServiceContent> lqw = buildQueryWrapper(bo);
        Page<ServiceContentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的服务内容列表
     *
     * @param bo 查询条件
     * @return 服务内容列表
     */
    @Override
    public List<ServiceContentVo> queryList(ServiceContentBo bo) {
        LambdaQueryWrapper<ServiceContent> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ServiceContent> buildQueryWrapper(ServiceContentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ServiceContent> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ServiceContent::getServiceContentId);
        lqw.eq(bo.getServiceItemId() != null, ServiceContent::getServiceItemId, bo.getServiceItemId());
        lqw.eq(StringUtils.isNotBlank(bo.getServiceContentDesc()), ServiceContent::getServiceContentDesc, bo.getServiceContentDesc());
        return lqw;
    }

    /**
     * 新增服务内容
     *
     * @param bo 服务内容
     * @return 是否新增成功
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean insertByBo(ServiceContentLevelReqBo bo) {
        //插入服务内容表
        ServiceContent serviceContent = new ServiceContent();
        serviceContent.setServiceItemId(bo.getServiceItemId());
        serviceContent.setServiceContentDesc(bo.getServiceContentDesc());
        boolean flag = baseMapper.insert(serviceContent) > 0;
        if (flag) {
            bo.setServiceContentId(serviceContent.getServiceContentId());
        }
        List<ServiceContentTalent> contentTalentList = bo.getTalentLevelList().stream().map(s -> {
            ServiceContentTalent serviceContentTalent = new ServiceContentTalent();
            serviceContentTalent.setServiceContentId(serviceContent.getServiceContentId());
            serviceContentTalent.setTalentValue(s);
            return serviceContentTalent;
        }).collect(Collectors.toList());
        serviceContentTalentMapper.insertBatch(contentTalentList);
        return flag;
    }

    /**
     * 修改服务内容
     *
     * @param bo 服务内容
     * @return 是否修改成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(ServiceContentLevelReqBo bo) {
        ServiceContent update = baseMapper.selectById(bo.getServiceContentId());
        update.setServiceContentDesc(bo.getServiceContentDesc());
        boolean updateResult = baseMapper.updateById(update) > 0;
        LambdaQueryWrapper<ServiceContentTalent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(bo.getServiceContentId() != null, ServiceContentTalent::getServiceContentId, bo.getServiceContentId());
        serviceContentTalentMapper.delete(queryWrapper);
        List<ServiceContentTalent> contentTalentList = bo.getTalentLevelList().stream().map(s -> {
            ServiceContentTalent serviceContentTalent = new ServiceContentTalent();
            serviceContentTalent.setServiceContentId(bo.getServiceContentId());
            serviceContentTalent.setTalentValue(s);
            return serviceContentTalent;
        }).collect(Collectors.toList());
        serviceContentTalentMapper.insertBatch(contentTalentList);
        return updateResult;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ServiceContent entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除服务内容信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public List<ServiceContentVo> findByServiceItemId(Long serviceItemId) {
        return baseMapper.findByServiceItemId(serviceItemId);
    }
}
